package cn.leaf.calculator.bean;

import cn.leaf.calculator.resolver.FormulaBeanDefinitionResolver;
import org.springframework.util.Assert;

import java.io.Serializable;
import java.util.Map;

/**
 * formula
 *
 * @author 李克国
 * @version 1.0.0

 */
public class StandardFormulaBean<T> implements FormulaBeanDefinition<T> {

    private Serializable id;

    private String name;

    private T value;

    private int state;

    private String formulaString;

    private String formulaName;

    private Map<Serializable, ParameterBeanDefinition<T>> parameters;

    private FormulaBeanDefinitionResolver<T> formulaBeanDefinitionResolver;

    @Override
    public void setId(Serializable id) {
        this.id = id;
    }

    @Override
    public void setName(String name) {
        this.name = name;
    }

    @Override
    public void setValue(T value) {
        this.value = value;
    }

    @Override
    public void setFormulaString(String formulaString) {
        this.formulaString = formulaString;
    }

    @Override
    public void setFormulaName(String formulaName) {
        this.formulaName = formulaName;
    }

    @Override
    public String getFormulaString() {
        return formulaString;
    }

    @Override
    public String getFormulaName() {
        return formulaName;
    }

    @Override
    public Map<Serializable, ParameterBeanDefinition<T>> getParameters() {
        return parameters;
    }

    @Override
    public boolean parameterContains(Serializable id) {
        return parameters.containsKey(id);
    }

    @Override
    public void setParameter(ParameterBeanDefinition<T> parameter) {
        Assert.notNull(parameter, "不允许设置为空的参数信息");
        parameters.put(parameter.getId(), parameter);
    }

    @Override
    public void resolve() {
        parameters = formulaBeanDefinitionResolver.resolve(formulaString);
    }

    @Override
    public void setResolver(FormulaBeanDefinitionResolver<T> resolver) {
        this.formulaBeanDefinitionResolver = resolver;
    }

    @Override
    public Serializable getId() {
        return id;
    }

    @Override
    public String getName() {
        return name;
    }

    @Override
    public T getValue() {
        return value;
    }

    @Override
    public void updateState(int state) {
        this.state = state;
    }

    @Override
    public int getState() {
        return state;
    }
}
